﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;
using ZhimeiSoft.Framework.Client.Control.DateTimeGrid;
using ZhimeiSoft.Framework.Client.Control;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class ReservationMonth : BaseUserControl
    {
        DataSet dsReservation = new DataSet();

        public DBEntityReservation ReservationInfo
        {
            get
            {
                if (grdViewScheduleMonth.FocusedRowHandle == -1)
                    return null;

                DataRow drReservation = grdViewScheduleMonth.GetDataRow(grdViewScheduleMonth.FocusedRowHandle);
                DBEntityReservation reservationInfo = new DBEntityReservation(drReservation);
                return reservationInfo;
            }
        }

        public ReservationMonth()
        {
            InitializeComponent();
            monthDateTimeForMonth.MonthDateTime = DateTime.Now;
            monthDateTimeForMonth.OnEventCurrMonth += this.OnMonthChange;
            monthDateTimeForMonth.OnEventClickCurrDay += this.EventClickCurrDay;
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshData()
        {
            OnMonthChange(monthDateTimeForMonth.MonthDateTime);
        }

        private void queryReservation(DateTime queryTime)
        {
            string error = string.Empty;
            if (BussinessProxyManager.Instance != null)
            {
                if (!BussinessProxyManager.Instance.QueryReservationMonth(queryTime.Year, queryTime.Month, string.Empty , out dsReservation, out error))
                {
                    MsgBox.MsgInfo("获取预约信息错误：" + error);
                    return;
                }
                monthDateTimeForMonth.DataBindForMonth(dsReservation, DBEntityCustomInfo.FieldName.CNICKNAME,
                    DBEntityReservation.FieldName.RESERVATIONID, DBEntityReservation.FieldName.RESERVATIONTIME);
            }
        }

        private void OnMonthChange(DateTime currMonth)
        {
            queryReservation(currMonth);
        }

        private void monthDateTimeForMonth_Load(object sender, EventArgs e)
        {
            if(monthDateTimeForMonth.CurrDateTimeViewDayControl == null)
                return;

            queryReservation(monthDateTimeForMonth.MonthDateTime);
        }

        private void EventClickCurrDay(object iDateTimeView)
        {
            MonthDateTimeForDay currDateTimeViewControl = (MonthDateTimeForDay)iDateTimeView;
            DateTime clickDate = currDateTimeViewControl.GetDateTime();
            DataSet dsReservationDay = new DataSet();
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryResveration(clickDate.Year, clickDate.Month, clickDate.Day, "", out dsReservationDay, out error))
            {
                MsgBox.MsgInfo("获取预约信息时出现错误：" + error);
                return;
            }
            grdViewScheduleMonth.BeginUpdate();
            try
            {
                grdCtrlScheduleMonth.DataSource = dsReservationDay.Tables[0];
            }
            finally
            {
                grdViewScheduleMonth.EndUpdate();
            }
        }
        private void monthDateTimeForMonth_Click(object sender, EventArgs e)
        {
        }

        private void grdViewScheduleMonth_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
            {
                return;
            }
            if (e.Column.FieldName.Equals(DBEntityReservation.FieldName.RSTATE))
            {
                if (e.CellValue == null || e.CellValue.ToString() == string.Empty)
                    return;

                string displayText = DBEntityReservation.ENUM_RSTATE.EnumStrToChineseStr(e.CellValue.ToString());
                if (displayText.Equals(string.Empty))
                    return;
                e.DisplayText = displayText;
            }
        }

        private void grdViewScheduleMonth_Click(object sender, EventArgs e)
        {
            

        }
    }
}
